package be.dch.objectprops.impl;

import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

import be.dch.objectprops.ObjectPropertiesStore;
import be.dch.objectprops.collector.Collector;

import com.google.inject.Inject;

@SuppressWarnings("unchecked")
public class ObjectPropertiesStoreImpl implements ObjectPropertiesStore {

    private static final Logger LOG = Logger.getLogger(ObjectPropertiesStoreImpl.class);
    @Inject
    private transient Collector props;
    @Inject
    private transient ObjectWriter writer;
    @Inject
    private transient ObjectReader reader;

    /*
     * (non-Javadoc)
     * 
     * @see be.dch.objectprops.ObjectPropertiesStore#getDatabase()
     */
    public Properties getDatabase() {
	return (Properties) this.props;
    }

    /*
     * (non-Javadoc)
     * 
     * @see be.dch.objectprops.ObjectPropertiesStore#clear()
     */

    public void clear() {
	this.props.clear();
    }

    /*
     * (non-Javadoc)
     * 
     * @see be.dch.objectprops.ObjectPropertiesStore#clean(java.lang.String)
     */
    public void clean(final String prefix) {
	final Set<Object> keySet = (Set<Object>) this.props.keySet();
	for (final Object key : keySet) {
	    if (key.toString().startsWith(prefix)) {
		ObjectPropertiesStoreImpl.LOG.info("Removing key:" + key + "from database");
		this.props.remove(key);
	    }
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * be.dch.objectprops.ObjectPropertiesStore#containsObjectAt(java.lang.String
     * )
     */
    public boolean containsObjectAt(final String prefix) {
	boolean result = false;
	final Set<Object> keySet = (Set<Object>) this.props.keySet();
	for (final Object key : keySet) {
	    if (key.toString().startsWith(prefix)) {
		result = true;
		break;
	    }
	}
	return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see be.dch.objectprops.ObjectPropertiesStore#readObject(java.lang.Class)
     */

    public Object readObject(final Class<?> clazz) {

	return this.reader.readObject(clazz);
    }

    /*
     * (non-Javadoc)
     * 
     * @see be.dch.objectprops.ObjectPropertiesStore#readObject(java.lang.Class,
     * java.lang.String)
     */

    public Object readObject(final Class<?> clazz, final String prefixInput) {

	return this.reader.readObject(prefixInput, clazz);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * be.dch.objectprops.ObjectPropertiesStore#writeObject(java.lang.Object)
     */

    public void writeObject(final Object object) {

	this.writer.writeObject(object);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * be.dch.objectprops.ObjectPropertiesStore#writeObject(java.lang.Object,
     * java.lang.String)
     */

    public void writeObject(final Object object, final String keyprefix) {

	this.writer.writeObject(keyprefix, object);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * be.dch.objectprops.ObjectPropertiesStore#setReader(be.dch.objectprops
     * .ObjectReader)
     */

    public void setReader(final ObjectReader reader) {
	this.reader = reader;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * be.dch.objectprops.ObjectPropertiesStore#setWriter(be.dch.objectprops
     * .ObjectWriter)
     */

    public void setWriter(final ObjectWriter writer) {
	this.writer = writer;
    }

}
